package com.digua.atools.tools;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.digua.atools.tools.listener.DataToAttrListener;
import com.google.android.flexbox.FlexboxLayout;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author:digua
 * @dat:2021/8/25
 * @description: view工具
 */
public class ViewUtil {
    private final static String tag = "ViewUtil";

    /**
     * 是否非重复点击View (少于500毫秒算重复点击)
     *
     * @param v
     * @return true:正常点击 false:重复点击
     */
    public static boolean isNotReClick(View v) {
        return isNotReClick(v, 500L);
    }

    /**
     * 是否非重复点击View
     *
     * @param v
     * @param reTime 重复时间
     * @return true:正常点击 false:重复点击
     */
    public static boolean isNotReClick(View v, long reTime) {
        return !isReClick(v, reTime);
    }

    /**
     * 是否重复点击View (少于500毫秒算重复点击)
     *
     * @param v
     * @param igViewIds 要忽略的viewId
     * @return true:重复点击 false:非重复点击
     */
    public static boolean isReClick(View v, Integer... igViewIds) {
        return isReClick(v, 500L, igViewIds);
    }

    /**
     * 是否重复点击View
     *
     * @param v
     * @param reTime    重复时间
     * @param igViewIds 要忽略的viewId
     * @return true:重复点击 false:非重复点击
     */
    public static boolean isReClick(View v, long reTime, Integer... igViewIds) {
        if (v == null) {
            return false;
        }
        if (igViewIds != null && igViewIds.length > 0) {
            List<Integer> igIds = Arrays.asList(igViewIds);
            if (igIds.contains(v.getId())) {
                return false;
            }
        }
        long time = SPUtils.getInstance().getLong(tag + v.getId(), 0L);
        int num = SPUtils.getInstance().getInt(tag + "num" + v.getId(), 0);
        long cut = System.currentTimeMillis();
        SPUtils.getInstance().put(tag + v.getId(), cut);
        if (time <= 0 || cut - time > reTime) {//不算重复操作
            SPUtils.getInstance().remove(tag + "num" + v.getId());
            return false;
        }
        SPUtils.getInstance().put(tag + "num" + v.getId(), ++num);
        if (num > 2) {
            ToastUtils.showShort("您的操作太频繁了");
        }
        return true;
    }

    /**
     * 获取控件值转Integer
     *
     * @param view
     * @param <V>
     * @return
     */
    public static <V extends TextView> Integer getValInteger(V view) {
        return getValInteger(view, null);
    }

    /**
     * 获取控件值转Integer
     *
     * @param view
     * @param <V>
     * @return
     */
    public static <V extends TextView> Integer getValInteger(V view, Integer defVal) {
        String val = getVal(view);
        if (com.digua.atools.tools.StrUtil.isBlank(val)) {
            return defVal;
        }
        return NumUtil.parseInteger(val, defVal);
    }

    /**
     * 获取控件值 为空值是Toast提示
     *
     * @param view
     * @param <V>
     * @return
     */
    public static <V extends TextView> Integer getValIntegerAndNullToast(V view, String errTitle) {
        Integer res = getValInteger(view);
        if (res == null && com.digua.atools.tools.StrUtil.isNotBlank(errTitle)) {
            Toast.makeText(view.getContext(), errTitle, Toast.LENGTH_SHORT).show();
            return null;
        }
        return res;
    }

    /**
     * 获取控件值
     *
     * @param view
     * @param <V>
     * @return
     */
    public static <V extends TextView> String getVal(V view) {
        if (view == null) {
            return null;
        }
        return view.getText().toString();
    }

    /**
     * 获取控件值
     *
     * @param view
     * @param defVal 默认值
     * @param <V>
     * @return
     */
    public static <V extends TextView> String getVal(V view, String defVal) {
        if (view == null) {
            return defVal;
        }
        return view.getText().toString();
    }

    /**
     * 获取控件值 为空值是Toast提示
     *
     * @param view
     * @param <V>
     * @return
     */
    public static <V extends TextView> String getValAndNullToast(V view, String errTitle) {
        String val = getVal(view);
        if (com.digua.atools.tools.StrUtil.isBlank(val)) {
            if (view != null && com.digua.atools.tools.StrUtil.isNotBlank(errTitle)) {
                Toast.makeText(view.getContext(), errTitle, Toast.LENGTH_SHORT).show();
            }
            return null;
        }
        return val;
    }

    /**
     * 获取控件值 为空值是Toast提示
     *
     * @param view
     * @return
     */
    public static void setVisible(View view, boolean isVisible) {
        if (view == null) {
            return;
        }
        view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取控件值 为空值是Toast提示
     *
     * @param view
     * @return
     */
    public static <T> void setVisible(View view, T data, DataToAttrListener<T, Boolean> listener) {
        setVisible(view, Utils.getBool(data, listener));
    }

    /**
     * FlexboxLayout布局，元素隔行变色
     *
     * @param layout FlexboxLayout容器
     * @param color1 色值1
     * @param color2 色值2
     */
    public static void setFlexboxInterlacedDiscoloration(FlexboxLayout layout, String color1, String color2) {
        if (layout == null) {
            return;
        }
        //属性隔行变色处理
        String color = color1;
        LinkedList<View> views = new LinkedList<>();
        for (int i = 0; i < layout.getChildCount(); i++) {
            if (layout.getChildAt(i).getVisibility() == View.VISIBLE) {
                views.add(layout.getChildAt(i));
            }
        }
        if (com.digua.atools.tools.CollUtil.isBlank(views)) {
            return;
        }
        for (int i = 0; i < views.size(); i++) {
            View view1 = views.get(i);
            view1.setBackgroundColor(Color.parseColor(color));
            FlexboxLayout.LayoutParams params1 = (FlexboxLayout.LayoutParams) view1.getLayoutParams();
            if (params1.getFlexBasisPercent() == 1) {
                color = com.digua.atools.tools.ObjUtil.eq(color, color1) ? color2 : color1;
                continue;
            }
            ++i;
            if (i >= views.size()) {
                return;
            }
            View view2 = views.get(i);
            FlexboxLayout.LayoutParams params2 = (FlexboxLayout.LayoutParams) view2.getLayoutParams();
            if (params2.getFlexBasisPercent() == 1) {
                color = com.digua.atools.tools.ObjUtil.eq(color, color1) ? color2 : color1;
            }
            view2.setBackgroundColor(Color.parseColor(color));
            color = com.digua.atools.tools.ObjUtil.eq(color, color1) ? color2 : color1;
        }
    }


    /**
     * FlexboxLayout布局，元素隔行变色
     *
     * @param layout FlexboxLayout容器
     */
    public static void setFlexboxInterlacedDiscoloration(FlexboxLayout layout) {
        setFlexboxInterlacedDiscoloration(layout, "#FFFFFF", "#F0F0F0");
    }
}
